//Erstellt von Moritz Liepe
package main.editor;

import main.TextControl;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;


public class EditHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		
		
		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
		if (selection != null & selection instanceof IStructuredSelection) 
		{
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
			if(! strucSelection.isEmpty() && strucSelection.getFirstElement() instanceof TextControl)
			{
				TextControl selectionObject = (TextControl)strucSelection.getFirstElement();
				
				Shell shell = HandlerUtil.getActiveWorkbenchWindow(event).getShell();
				EditModuleDialog emd= new EditModuleDialog(shell, selectionObject);
					if (emd.open() == Window.OK) 
					{
						VOSEditor vosEditor;
						IEditorPart page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
						if((page != null) && (page instanceof VOSEditor))
						{
							vosEditor = (VOSEditor)page;
							vosEditor.setDirty(true);
						}
					}
				/*else {
					Shell shell = HandlerUtil.getActiveWorkbenchWindow(event).getShell();  
					String dialogBoxTitle = "Result";  
					MessageDialog.openInformation(shell, dialogBoxTitle, "There are no editable Fields in this Module!"); 
				}*/
				
			}
			
		}
		return null;
	}

}
